package com.xls.box2023.home;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzz.bottomencapsulation.customview.statelayout.StateLayout;
import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.utils.SPUtils;
import com.lzz.bottomencapsulation.utils.TimeUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xls.box2023.R;
import com.xls.box2023.customcontrol.RoundedImageView;
import com.xls.box2023.entity.AccountBalanceEntity;
import com.xls.box2023.entity.UserEntity;
import com.xls.box2023.jifen.JiFenActivity;
import com.xls.box2023.jinbi.JinBiActivity;
import com.xls.box2023.login.LoginActivity;
import com.xls.box2023.login.LoginUtil;
import com.xls.box2023.mvp.MVPBaseActivity;
import com.xls.box2023.tuiguangtuandui.TuiGuangTuanDuiActivity;
import com.xls.box2023.wxshare.WXShareActivity;
import com.xls.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 首页
 */
public class HomeActivity extends MVPBaseActivity<HomeContract.View, HomePresenter> implements HomeContract.View, View.OnClickListener, XRecyclerView.LoadingListener {

    private RoundedImageView home_head;
    private TextView home_nicename;
    private TextView home_jinbi;
    private TextView home_jifen;
    private LinearLayout home_numcontent;
    private TextView home_youxi_title;
    private XRecyclerView home_list;
    private TextView home_tuichudenglu;
    private StateLayout home_list_state;
    private RelativeLayout home_ll_jinbi;
    private RelativeLayout home_ll_jifen;
    private TextView home_share;
    private TextView home_tuiguangtuandui;
    private RelativeLayout game_touxiang_img;

    @Override
    public int getView() {
        return R.layout.activity_home;
    }

    public static void  startActivity(Context context){
        Intent intent=new Intent(context,HomeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseApplication.getAppManager().finishOtherActivity(HomeActivity.class);
        if (TextUtils.isEmpty(SPUtils.getLoginToken())||TextUtils.isEmpty(com.xls.box2023.utils.SPUtils.getunId())){
            LoginActivity.startActivity(getActivity());
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!TextUtils.isEmpty(SPUtils.getLoginToken())&&!TextUtils.isEmpty(com.xls.box2023.utils.SPUtils.getunId())){
            mPresenter.getPermissions();//风控
        }
    }

    @Override
    public void initView() {
        home_head = findViewById(R.id.home_head);
        home_nicename = findViewById(R.id.home_nicename);
        home_jinbi = findViewById(R.id.home_jinbi);
        home_jifen = findViewById(R.id.home_jifen);
        home_numcontent = findViewById(R.id.home_numcontent);
        home_youxi_title = findViewById(R.id.home_youxi_title);
        home_list = findViewById(R.id.home_list);

        setAdapter();
        home_list.setLoadingListener(this);
        home_list.setLoadingMoreEnabled(false);
        home_list.setPullRefreshEnabled(false);
        home_tuichudenglu = findViewById(R.id.home_tuichudenglu);
        home_tuichudenglu.setOnClickListener(this);

        home_list_state = findViewById(R.id.home_list_state);
        home_list_state.showProgressView();
        home_list_state.setErrorAndEmptyAction(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                home_list_state.showProgressView();
                mPresenter.getSourceData();
            }
        });
        home_ll_jinbi = findViewById(R.id.home_ll_jinbi);
        home_ll_jifen = findViewById(R.id.home_ll_jifen);
        home_ll_jinbi.setOnClickListener(this);
        home_ll_jifen.setOnClickListener(this);
        home_share = findViewById(R.id.home_share);
        home_share.setOnClickListener(this);
        home_tuiguangtuandui = findViewById(R.id.home_tuiguangtuandui);
        home_tuiguangtuandui.setOnClickListener(this);
        game_touxiang_img = findViewById(R.id.game_touxiang_img);
    }

    /**
     * 设置游戏列表适配器
     */
    private void setAdapter() {
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        home_list.setLayoutManager(gridLayoutManager);
        home_list.setAdapter(mPresenter.getYouXiListAdapter());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.home_ll_jinbi) {
            JinBiActivity.startActivity(getActivity());
        } else if (id == R.id.home_ll_jifen) {
            JiFenActivity.startActivity(getActivity());
        }else if (id == R.id.home_tuichudenglu) {
            LoginUtil.getInstance().outLogin(this);
        }else  if (id == R.id.home_share) {
            WXShareActivity.start(this);
        }else  if (id == R.id.home_tuiguangtuandui) {
            TuiGuangTuanDuiActivity.start(this, TimeUtil.getText(TimeUtil.yMd,TimeUtil.getTimeMliiis()),TimeUtil.getText(TimeUtil.yMd,TimeUtil.getTimeMliiis()),requestData.getRefferScoreTotal());
        }
    }
    AccountBalanceEntity requestData;
    @Override
    public void showNum(AccountBalanceEntity requestData) {
        this.requestData=requestData;
        double jinbi =Double.parseDouble(requestData.getCoins());
        BigDecimal number=new BigDecimal(""+jinbi);
        home_jinbi.setText( number.setScale(1,BigDecimal.ROUND_DOWN) + " 金币");
        home_jifen.setText(requestData.getFxScoreText()+" 元宝");
    }

    @Override
    public void showUserInfo(UserEntity requestData) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(false)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
         .build();
        ImageLoader.getInstance().displayImage(requestData.getAvatar(),home_head, options);
        if (!TextUtils.isEmpty(requestData.getNickname()))  home_nicename.setText(requestData.getNickname());
    }

    @Override
    public void stopRefishView() {
        home_list.refreshComplete();
        home_list.loadMoreComplete();
    }

    @Override
    public StateLayout getStateView() {
        return home_list_state;
    }

    @Override
    public RecyclerView getListview() {
        return home_list;
    }

    @Override
    public void showShareBtn(boolean b) {
        if (b){
            home_share.setVisibility(View.VISIBLE);
        }else {
            home_share.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onRefresh() {
        mPresenter.onRefresh();
    }

    @Override
    public void onLoadMore() {
        mPresenter.onLoadMore();
    }

//    @Override
//    public void onEvent(MessageEvent messageEvent) {
//        super.onEvent(messageEvent);
////        if (messageEvent.getCode()== EventbusCode.CODE_TIXIAN_OK){
////            mPresenter.getAccountBalance();
////        }
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        super.onEvent(messageEvent);
        mPresenter.refishAPPData(messageEvent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.stopDownloadFile();
    }
}
